home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Belgian Amiga Club - ADF Collection
/
BS1 part 65.zip
/
BS1 part 65
/
Math Visin v2.1 disk 1.adf
/
Arexx.WB
/
Misc
/
Recursion
< prev
next >
Wrap
Text File
|
1992-02-12
|
4KB
|
129 lines
/* Recursion an interactive demonstration 14-Mar-90 dh
An interactive example of recursion.
--------------------------------------------------------------------------- */
OPTIONS RESULTS
ClearScreen = d2c(12)
SAY ClearScreen
SAY "This is a short example of recursion, utilizing a Power function. Below"
SAY "are two versions, one in pseudocode, the other in MathVision syntax. They"
SAY "both calculate 3 to the fourth power, 3^4. Take a moment to compare them."
SAY ""
SAY "Answer = Power(3,4) INVOCATION OF POWER FUNCTION"
SAY "Power(num,pow) DEFINITION OF POWER FUNCTION"
SAY " IF pow>0"
SAY " THEN return num*Power(num,pow-1)"
SAY " ELSE return 1"
SAY ""
SAY ""
SAY ""
SAY "VAL: fa(3,4) MATHVISION INVOCATION OF POWER FUNC"
SAY "FA: if( a2>0, a1*fa(a1,a2-1), 1 ) MATHVISION DEFINITION OF POWER FUNC"
ok = WaitForReturn("When done looking, Press <RETURN> ")
SAY ClearScreen||"This screen explains the labels on the example."
SAY
SAY "EXPANSION: Power Expansion shown in upper lefthand corner"
SAY ""
SAY "NUMBER & POWER: These columns show the values which were passed to the"
SAY " current invocation of the power function. They correspond"
SAY " with A1 and A2 in MathVision."
SAY ""
SAY "CALLS: When a call is made to the Power function, it is noted here."
SAY ""
SAY "CALCULATION: Show the calculations performed by this invocation."
SAY ""
SAY "RETURNS: Show the value returned by this invocation."
ok = WaitForReturn("My thinking cap is ON. Press <RETURN> ")
Say ClearScreen
Number = GetAns("Enter a Number",3)
Power = GetAns("Enter a Power", 4)
if (power<0) THEN power = 0
if (power>10) THEN power = 10
ok = WaitForReturn("We are going to calculate "Number"^"Power" Press <RETURN> ")
Say ClearScreen
SAY xy(1,3)||"NUMBER POWER CALLS"xy(40,3)"CALCULATIONS"||xy(55,3)"RETURN"
ok = StartPower(Number,Power)
EXIT
/*------------------------------- StartPower ---------------------------- */
StartPower:
ARG Number, Power
LineNum = 4
SAY xy(20,LineNum)||"Power("Number","Power")"
Answer = DoPower(Number,Power,LineNum+1, 0)
say xy(52,LineNum)" = "Answer
ok = WaitForReturn("The answer is "Answer". Press <RETURN> ")
Say ClearLine(LineNum)
RETURN Answer
/*------------------------------- DoPower ------------------------------- */
DoPower: PROCEDURE
ARG Number, Power, LineNum, Invocation
say ClearLine(1)
if (Invocation=0)
THEN Expansion = xy(1,1)"Expansion: "
ELSE Expansion = xy(1,1)"Expansion: "||Space(Copies(" "||Number,invocation),1,"*")||"*"
Say Expansion||Number"^"Power
say xy(3,LineNum)||Number||xy(12,LineNum)||Power
IF (Power <= 0)
THEN
DO
val = 1
say xy(37,LineNum)||" = 1"xy(52,LineNum)" = "Val
ok = WaitForReturn("Recursion limit reached: Returning 1. Press <RETURN> ")
Say ClearLine(1)||Expansion||"1"
END
ELSE
DO
ok = WaitForReturn("Need to evaluate Power("Number","Power-1") Press <RETURN> ")
say xy(20,LineNum)||Number"*Power("Number","Power-1")"
val = DoPower(Number,Power-1,LineNum+1,Invocation+1)
say xy(37,LineNum)||" = "||Number||"*"Val||xy(52,LineNum)" = "Val*Number
val = val * number
ok = WaitForReturn("Returning "Val". Press <RETURN> " )
Say ClearLine(1)||Expansion||Val
END
Say ClearLine(LineNum)
RETURN val
/*------------------------------ WaitForReturn -------------------------- */
WaitForReturn:
PARSE ARG TellMe
len = Length(TellMe)
Options Prompt xy(77-len, 20)||TellMe
PULL response
say xy(77-len,20)||Copies(" ",len)
RETURN response
/*------------------------------ ClearLine ------------------------------ */
ClearLine: /* return string which clears a given line */
Arg line
Return xy(1,line)||Copies(" ",78)||xy(1,line)
/*---------------------------------- XY --------------------------------- */
XY: /* return string which sets cursor to x,y coordinate */
ARG x,y
RETURN d2c(155)||y||";"||x"H"
/* --------------------------------- GetAns -------------------------------- */
/* result = GetAns( prompt, default) */
/* prompt for input, displaying default. Accept one item. If only <return> */
/* was pressed, return the default answer */
GetAns:
parse arg prompter, default
Options Prompt Prompter "(" Default "): "
pull response
if (response = "") THEN response = default
return(response)